class Solution:
    def countPairs(self, deliciousness: list) -> int:
        MOD = 10**9 + 7
        sums = []
        for i in range(22):
            sums.append(2**i)
        indexMap = {}
        for i in range(len(deliciousness)):
            if deliciousness[i] not in indexMap:
                indexMap[deliciousness[i]] = set()
            indexMap[deliciousness[i]].add(i)
        res = 0
        for i in range(len(deliciousness)):
            num = deliciousness[i]
            indexMap[num].remove(i)
            for s in sums:
                diff = s - num
                if diff in indexMap:
                    res += len(indexMap[diff])
                    res %= MOD

        return res
